﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Security.Cryptography;
using System.Text;

namespace BlogDH
{
    public class CommMethod
    {

        public static string MD5Hash(string strText)
        {
            return MD5Hash(strText, Encoding.Default);
        }

        public static string GetCookieValue(string key)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies[key];
            if (cookie != null)
            {
                return cookie.Value;
            }
            return null;
        }

        public static void SetCookieValue(string key, object value, DateTime dt)
        {
            if (value == null)
            {
                HttpContext.Current.Response.Cookies.Remove(key);
            }
            else
            {
                HttpCookie cookie = HttpContext.Current.Request.Cookies[key];
                if (cookie != null)
                {
                    cookie.Value = value.ToString();
                    //cookie.Domain = domain;
                    cookie.Expires = dt;
                    HttpContext.Current.Response.AppendCookie(cookie);
                }
                else
                {
                    cookie = new HttpCookie(key, value.ToString());
                    //cookie.Domain = domain;
                    cookie.Expires = dt;
                    HttpContext.Current.Response.AppendCookie(cookie);
                }
            }
        }

        private static string MD5Hash(string strText, Encoding encoding)
        {
            MD5 md = new MD5CryptoServiceProvider();
            byte[] bytes = encoding.GetBytes(strText);
            byte[] buffer2 = md.ComputeHash(bytes);
            string str = null;
            for (int i = 0; i < buffer2.Length; i++)
            {
                string str2 = buffer2[i].ToString("x");
                if (str2.Length == 1)
                {
                    str2 = "0" + str2;
                }
                str = str + str2;
            }
            return str;
        }

        public static string GetAreaHtml(string ename="")
        {
            List<Model.Area> areas = CommValue.AreaList;
            System.Text.StringBuilder b = new System.Text.StringBuilder();
            if (string.IsNullOrEmpty(ename))
            {
                for (int i = 0; i < areas.Count; i++)
                {
                    b.AppendFormat("<li><a href=\"/area/{0}.html\">{1}</a></li>", areas[i].Ename, areas[i].Name);
                }
            }
            else
            {
                for (int i = 0; i < areas.Count; i++)
                {
                    if (areas[i].Ename == ename)
                    {
                        b.AppendFormat("<li style='background-color: #1570a6'><a style='color: #fff' href=\"/area/{0}.html\">{1}</a></li>", areas[i].Ename, areas[i].Name);
                    }
                    else
                    {
                        b.AppendFormat("<li><a href=\"/area/{0}.html\">{1}</a></li>", areas[i].Ename, areas[i].Name);
                    }
                }
            }

                return b.ToString();
        }

        public static string GetPaging(UrlParams param, int count, int curPageIndex)
        {
            //&laquo;&raquo;&hellip;
            string prev = "<a class=\"prev page-numbers\" href=\"{0}\">&laquo;</a>";
            string numStr = "<a class='page-numbers' href='{0}'>{1}</a>";
            string cur = "<span class='page-numbers current'>{0}</span>";
            string lue = "<span class=\"page-numbers dots\">&hellip;</span>";
            string next = "<a class=\"next page-numbers\" href=\"{0}\">&raquo;</a>";

            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            if (curPageIndex != 1)
                sb.AppendFormat(prev, GetUrl(param, curPageIndex - 1));        //上一页

            if (curPageIndex != 1)
                sb.AppendFormat(numStr, GetUrl(param, 1), 1); //第一页

            if ((curPageIndex - 2) > 2 && (curPageIndex - 2) < count)
                sb.Append(lue); //省略号

            if ((curPageIndex - 2) > 1 && (curPageIndex - 2) < count)
                sb.AppendFormat(numStr, GetUrl(param, curPageIndex - 2), curPageIndex - 2);   //当前页的前一页

            if ((curPageIndex - 1) > 1 && (curPageIndex - 1) < count)
                sb.AppendFormat(numStr, GetUrl(param, curPageIndex - 1), curPageIndex - 1);   //当前页向前第二页

            //if (curPageIndex > 0)
            sb.AppendFormat(cur, curPageIndex);    //当前页

            if ((curPageIndex + 1) < count)
                sb.AppendFormat(numStr, GetUrl(param, curPageIndex + 1), curPageIndex + 1); //当前页的下一页

            if ((curPageIndex + 2) < count)
                sb.AppendFormat(numStr, GetUrl(param, curPageIndex + 2), curPageIndex + 2); //当前页向后的第二页

            if ((curPageIndex + 2) < count)
                sb.Append(lue);

            if (count > curPageIndex)
                sb.AppendFormat(numStr, GetUrl(param, count), count);    //最后一页

            if (count != curPageIndex)
                sb.AppendFormat(next, GetUrl(param, curPageIndex + 1)); //下一页

            return sb.ToString();
        }

        private static string GetUrl(UrlParams param, int curPageindex)
        {
            StringBuilder builder = new StringBuilder();

            if (param == null)
            {
                if (curPageindex <= 1)
                    builder.Append("/index.html");
                else
                    builder.AppendFormat("/index_{0}.html", curPageindex);
            }
            else
            {
                if (!string.IsNullOrEmpty(param.Area))
                {
                    if (curPageindex <= 1)
                        builder.AppendFormat("/area/{0}.html", param.Area);
                    else
                        builder.AppendFormat("/area/{0}_{1}.html", param.Area, curPageindex);
                }
            }

            return builder.ToString();
        }
    }

    public class UrlParams
    {
        ///// <summary>
        ///// 当前页面类型
        ///// </summary>
        //public LstPageType PageType { get; set; }

        /// <summary>
        /// 地区
        /// </summary>
        public string Area { get; set; }

        /// <summary>
        /// 源码类型
        /// </summary>
        public string CodeType { get; set; }
    }

    public enum LstPageType
    {
        Index,
        List,
        Search
    }
}